<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="奖惩类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rewardPunishCategory">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="list"
                v-model="model.rewardPunishCategory"
                dictCode="reward_punish_category"
                @input="onRewardPunishCategoryInput"
                placeholder="请选择奖惩类别"/>
            </a-form-model-item>
          </a-col>
          <template v-if="model.rewardPunishCategory == 1">
            <a-col :span="12">
              <a-form-model-item label="奖励考核积分" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rewardIntegral">
                <a-input-number
                  style="width: 100%"
                  :max="99999999"
                  :min="0"
                  :disabled="formDisabled"
                  v-model="model.rewardIntegral"
                  placeholder="请输入奖励考核积分"></a-input-number>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="奖励金额(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rewardMoney">
                <a-input-number
                  :disabled="formDisabled"
                  :max="99999999"
                  :min="0"
                  v-model="model.rewardMoney"
                  placeholder="请输入奖励金额"
                  style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="其他金额(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherMoney">
                <a-input-number
                  :disabled="formDisabled"
                  :max="99999999"
                  :min="0"
                  v-model="model.otherMoney"
                  placeholder="请输入其他金额"
                  style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="奖励事项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reason">
                <a-textarea :disabled="formDisabled" v-model="model.reason" rows="4" :maxLength="500" placeholder="请输入奖励事项"/>
              </a-form-model-item>
            </a-col>
          </template>
          <template v-if="model.rewardPunishCategory == 2">
            <a-col :span="12">
              <a-form-model-item label="扣除考核积分" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="punishmentIntegral">
                <a-input-number
                  style="width: 100%"
                  :max="99999999"
                  :min="0"
                  :disabled="formDisabled"
                  v-model="model.punishmentIntegral"
                  placeholder="请输入扣除考核积分"></a-input-number>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="处罚金额(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="punishmentMoney">
                <a-input-number
                  :disabled="formDisabled"
                  :max="99999999"
                  :min="0"
                  v-model="model.punishmentMoney"
                  placeholder="请输入处罚金额"
                  style="width: 100%" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="处罚事项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="method">
                <a-textarea :disabled="formDisabled" v-model="model.method" rows="4" :maxLength="500" placeholder="请输入处罚事项"/>
              </a-form-model-item>
            </a-col>
          </template>

          <a-col :span="12">
            <a-form-model-item label="发文单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="institution">
              <select-Company-input :disabled="formDisabled" placeholder="请选择发文单位" v-model="model.institution"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="文号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dispatch">
              <a-input :disabled="formDisabled" v-model="model.dispatch" placeholder="请输入文号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="发文日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="occurrenceDate">
              <j-date :disabled="formDisabled" placeholder="请选择奖惩日期" v-model="model.occurrenceDate" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { validateBlankValue } from '@/utils/util'
import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
import { mapGetters } from 'vuex';
export default {
  name: 'ErpEmployeeRewardPunishFileForm',
  components: {
    SelectCompanyInput
  },
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    employeeId: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {
        rewardPunishCategory: [
          { required: true, message: '请选择奖惩类别!' },
          { validator: (rule, value, callback) => validateBlankValue('erp_employee_reward_punish_file', 'reward_punish_category', value, this.model.id, callback) }
        ],
        rewardIntegral: [
          { required: true, message: '请输入奖励考核积分!' }
        ],
        rewardMoney: [
          { required: true, message: '请输入奖励金额!' },
          { required: true, pattern: /^(([0-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!' }
        ],
        otherMoney: [
          { required: true, message: '请输入其他金额!' },
          { required: true, pattern: /^(([0-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!' }
        ],

        punishmentIntegral: [
          { required: true, message: '请输入扣除考核积分!' }
        ],
        punishmentMoney: [
          { required: true, message: '请输入处罚金额!' },
          { required: true, pattern: /^(([0-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!' }
        ],
        institution: [
          { required: true, message: '请选择发文单位!' },
          { validator: (rule, value, callback) => validateBlankValue('erp_employee_reward_punish_file', 'institution', value, this.model.id, callback) }
        ],
        occurrenceDate: [
          { required: true, message: '请选择发文日期!' }
        ]
      },
      url: {
        add: '/api/erp/files/employee/erpEmployeeRewardPunishFile/add',
        edit: '/api/erp/files/employee/erpEmployeeRewardPunishFile/edit',
        queryById: '/api/erp/files/employee/erpEmployeeRewardPunishFile/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    ...mapGetters(['userInfo']),
    add() {
      this.edit(this.modelDefault);
      this.judge()
    },
    edit(record) {
      this.$refs.form.clearValidate()
      this.model = Object.assign({}, record);
      this.visible = true;
      this.judge(this.model.rewardPunishCategory)
    },
    onRewardPunishCategoryInput (event) {
      this.$set(this.model, 'rewardIntegral', null) // 奖励考核积分
      this.$set(this.model, 'rewardMoney', null) // 奖励金额
      this.$set(this.model, 'otherMoney', null) // 其他金额
      this.$set(this.model, 'reason', null) // 奖励事项
      this.$set(this.model, 'punishmentIntegral', null) // 扣除考核积分
      this.$set(this.model, 'punishmentMoney', null) // 处罚金额
      this.$set(this.model, 'method', null) // 处罚事项
      this.judge(event)
    },
    // 判断奖惩类别判断是否必填
    judge(value) {
      if (value == '1') {
        // 奖惩类别  - 奖励
        this.validatorRules.rewardMoney[0].required = true // 奖励金额
        this.validatorRules.rewardMoney[1].required = true // 奖励金额
        this.validatorRules.otherMoney[0].required = true // 其他金额
        this.validatorRules.otherMoney[1].required = true // 其他金额
        this.validatorRules.rewardIntegral[0].required = true // 奖励考核积分
        this.validatorRules.punishmentIntegral[0].required = false // 扣除考核积分
        this.validatorRules.punishmentMoney[0].required = false // 处罚金额
        this.validatorRules.punishmentMoney[1].required = false // 处罚金额
      } else if (value == '2') {
        // 奖惩类别  - 处罚
        this.validatorRules.rewardMoney[0].required = false // 奖励金额
        this.validatorRules.rewardMoney[1].required = false // 奖励金额
        this.validatorRules.otherMoney[0].required = false // 其他金额
        this.validatorRules.otherMoney[1].required = false // 其他金额
        this.validatorRules.rewardIntegral[0].required = false // 奖励考核积分
        this.validatorRules.punishmentIntegral[0].required = true // 扣除考核积分
        this.validatorRules.punishmentMoney[0].required = true // 处罚金额
        this.validatorRules.punishmentMoney[1].required = true // 处罚金额
      } else {
        this.validatorRules.rewardMoney[0].required = false // 奖励金额
        this.validatorRules.rewardMoney[1].required = false // 奖励金额
        this.validatorRules.otherMoney[0].required = false // 其他金额
        this.validatorRules.otherMoney[1].required = false // 其他金额
        this.validatorRules.rewardIntegral[0].required = false // 奖励考核积分
        this.validatorRules.punishmentIntegral[0].required = false // 扣除考核积分
        this.validatorRules.punishmentMoney[0].required = false // 处罚金额
        this.validatorRules.punishmentMoney[1].required = false // 处罚金额
      }
      this.$refs.form.clearValidate(['rewardMoney', 'otherMoney', 'punishmentMoney', 'rewardIntegral', 'punishmentIntegral']);
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          // that.confirmLoading = true;
          // let httpurl = '';
          // let method = '';
          // if (!this.model.id) {
          //   httpurl += this.url.add;
          //   method = 'post';
          // } else {
          //   httpurl += this.url.edit;
          //   method = 'put';
          // }
          // httpAction(httpurl, this.model, method).then((res) => {
          //   if (res.success) {
          //     that.$message.success(res.message);
          //     that.$emit('ok');
          //   } else {
          //     that.$message.warning(res.message);
          //   }
          // }).finally(() => {
          //   that.confirmLoading = false;
          // })
          this.$set(this.model, 'employeeId', this.employeeId);
          that.$emit('ok', that.model);
        }
      })
    }
  }
}
</script>
